require.config({
	paths:{
		'jquery':'jquery',
		'cookie':'jquery.cookie'
	}
})
require(['jquery','cookie'],function($,cookie){
	$(function(){
		//轮播图
		//全部商品分类鼠标滑过效果
		$('.list>div').hover(function(){
			$(this).css({'background':'white'});
			$(this).children().children().css({'color':'black'});
			if($(this).children().children().children()){
				$(this).children().children().children().css({'color':'black'});
			}					
		},function(){
			$(this).css({'background':'rgb(59,44,51)'});
			$('.list>div a').css({'color':'white'});
			$('.list>div a span').css({'color':'white'});
		})
		//导航栏鼠标划入效果
		$('.jlg_slideshow_top_list a').hover(function(){
			$(this).css('color','rgb(233,20,86)');
		},function(){
			$(this).css('color','rgb(68,68,68)');
		})
		//导航栏鼠标点击
//		$('.jlg_slideshow_top_list a').click(function(){
//			console.log($(this));
//			$(this).parent().addClass('listxue');
//		})
		$('.jlg_slideshow_top_list li:nth-child(1) a').click(function(){
			//console.log($(this));
			$(this).css('color','rgb(233,20,86)');
			$('.jlg_slideshow_top_list li:nth-child(2) a').css('color','rgb(68,68,68)');
			$('.jlg_slideshow_top_list li:nth-child(3) a').css('color','rgb(68,68,68)');
			$('.jlg_slideshow_top_list li:nth-child(4) a').css('color','rgb(68,68,68)');
			$(this).hover(function(){
				$(this).css('color','rgb(233,20,86)');
			},function(){
				$(this).css('color','rgb(233,20,86)');
			})
		});
		$('.jlg_slideshow_top_list li:nth-child(2) a').click(function(){
			//console.log($(this));
			$(this).css('color','rgb(233,20,86)');
			$('.jlg_slideshow_top_list li:nth-child(1) a').css('color','rgb(68,68,68)');
			$('.jlg_slideshow_top_list li:nth-child(3) a').css('color','rgb(68,68,68)');
			$('.jlg_slideshow_top_list li:nth-child(4) a').css('color','rgb(68,68,68)');
			$(this).hover(function(){
				$(this).css('color','rgb(233,20,86)');
			},function(){
				$(this).css('color','rgb(233,20,86)');
			})
		});
		$('.jlg_slideshow_top_list li:nth-child(3) a').click(function(){
			//console.log($(this));
			$(this).css('color','rgb(233,20,86)');
			$('.jlg_slideshow_top_list li:nth-child(1) a').css('color','rgb(68,68,68)');
			$('.jlg_slideshow_top_list li:nth-child(2) a').css('color','rgb(68,68,68)');
			$('.jlg_slideshow_top_list li:nth-child(4) a').css('color','rgb(68,68,68)');
			$(this).hover(function(){
				$(this).css('color','rgb(233,20,86)');
			},function(){
				$(this).css('color','rgb(233,20,86)');
			})
		});
		$('.jlg_slideshow_top_list li:nth-child(4) a').click(function(){
			//console.log($(this));
			$(this).css('color','rgb(233,20,86)');
			$('.jlg_slideshow_top_list li:nth-child(1) a').css('color','rgb(68,68,68)');
			$('.jlg_slideshow_top_list li:nth-child(3) a').css('color','rgb(68,68,68)');
			$('.jlg_slideshow_top_list li:nth-child(2) a').css('color','rgb(68,68,68)');
			$(this).hover(function(){
				$(this).css('color','rgb(233,20,86)');
			},function(){
				$(this).css('color','rgb(233,20,86)');
			})
		});
		//终极轮播图
		//ajax函数
		function ajax(url,fnWin,fnFailed){
			var xhl = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
			xhl.open('GET',url,true);
			xhl.send();
			xhl.onreadystatechange = function(){
				if(xhl.readyState == 4){
					if(xhl.status == 200){
						fnWin && fnWin(xhl.responseText);
					}else{
						fnFailed && fnFailed();
					}
				}
			}
		}
		//轮播图
		//获取图片
		ajax('json/index.json',function(img){
			img = new Function("return" + img)();
			for(var n = 0;n < img.length;n ++){
				var oLi = document.createElement('li');
				var oA = document.createElement('a');
				var oImg = document.createElement('img');
				oImg.src = "images/jlg-home/img" + (n + 1) + ".jpg";
				oA.appendChild(oImg);
				oLi.appendChild(oA);				
				$(".jlg_slideshow_pics").append(oLi);
			}
			var oAs = $('.jlg_slideshow_pics li a');				
//		//创建小圆圈并添加事件		
			var arr = [];
			var ol = document.createElement('ol');
			ol.style.height = 35 + 'px';
			for(var i = 0;i < oAs.length;i ++){
				var circle = document.createElement('div');
				circle.innerHTML = i + 1;
				circle.className = 'circles';
				arr.push(circle);
				ol.appendChild(circle);
			}
			$(".jlg_slideshow_pics").append(ol);
			var oldivs = $('ol div');
			//console.log(oldivs);
			//小圆圈移入
			for(var j = 0;j < oldivs.length;j ++){
				oldivs[j].index = j;
				oldivs[j].onmousemove = function(){
					clearInterval($timer);
					$nowPic = this.index;
					init();
				}
				oldivs[j].onmouseout = function(){
					autoPlay();
				}
			}
			//当前下标
			var $zIndex = 1;
			//当前图片的下标
			var $nowPic = 0;
			//定义定时器
			var $timer = null;
			init();
			autoPlay();
			//图片切换
			function init(){
				oAs[$nowPic].style["z-index"] = ++ $zIndex;
				for(var m = 0;m < oAs.length;m ++){
					sport(oAs[m],{opacity:0});
				}
				sport(oAs[$nowPic],{opacity:100});
				for(var k = 0;k < oldivs.length;k ++){
					oldivs[k].index = k;
					oldivs[k].style.background = 'black';
					oldivs[$nowPic].style.background = 'rgb(233,20,84)';				
				}			
			}
			//自动轮播
			function autoPlay(){
				$timer = setInterval(function(){
					$nowPic ++;
					if($nowPic > oAs.length - 1){
						$nowPic = 0;					
					}
					init();
				},3000)
			}
		})
		//运动函数
		function getStyle(obj,attr){
			return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj,1)[attr];
		}
		function sport(obj,json,fn){
			clearInterval(obj.timer);
			obj.timer = setInterval(function(){
				var stop = true;
				for(var attr in json){
					var cur = attr === "opacity" ? parseInt(parseFloat(getStyle(obj,attr)) * 100) : parseInt(getStyle(obj,attr));
					var speed = (json[attr] - cur) / 8;
					speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
					if(cur != json[attr]){
						stop = false;
					}
					if(attr == "opacity"){
						obj.style.opacity = (cur + speed) / 100;
						obj.style.filter = "alpha(opacity=" + (cur + speed) + ")";
					}else{
						obj.style[attr] = cur + speed + "px";
					}
				}
				if(stop){
					clearInterval(obj.timer);
					fn && fn();
				}
			},30);
		}
		//轮播图右边列表图
		//获取图片及其信息
		$.getJSON("json/index1.json",function(json){
			//console.log(json.onlinetext.img);
			$('<img>').attr("src",json.onlinetext.img).appendTo($('.listright .onlineImg a'));
			$(".listright .onlineImg a img").css({"width":"160px"});
			$(".listright .onlineImg").css({"padding":"15px"});
			$('.listright .jieshao a').append(json.onlinetext.name)
			$(".listright .price").append(json.onlinetext.price)
		})
	})	
	//商品列表
	$('.jlg_list_top .jlg_list_top_l').click(function(){
		$(this).children().css("color","rgb(233,20,86)")
		$('.jlg_list_top .jlg_list_top_r').children().css("color","black")
	})
	$('.jlg_list_top .jlg_list_top_r').click(function(){
		$(this).children().css("color","rgb(233,20,86)")
		$('.jlg_list_top .jlg_list_top_l').children().css("color","black")
	})
	//右侧轮播
	$.getJSON("json/index1.json",function(json){
		//console.log(json.imgWrap1_div1.img);
		$('<img>').attr("src",json.imgWrap1_div1.img).appendTo($('.midslideshow .imgWrap1 .warpimg1 a'));
		$('.midslideshow .warpjieshao1 a').append(json.imgWrap1_div1.name)
		$(".midslideshow .warpprice1").append(json.imgWrap1_div1.price)
		
		$('<img>').attr("src",json.imgWrap1_div2.img).appendTo($('.midslideshow .imgWrap2 .warpimg2 a'));
		$('.midslideshow .warpjieshao2 a').append(json.imgWrap1_div2.name)
		$(".midslideshow .warpprice2").append(json.imgWrap1_div2.price)
		
		$('<img>').attr("src",json.imgWrap1_div3.img).appendTo($('.midslideshow .imgWrap3 .warpimg3 a'));
		$('.midslideshow .warpjieshao3 a').append(json.imgWrap1_div3.name)
		$(".midslideshow .warpprice3").append(json.imgWrap1_div3.price)
		
		$('<img>').attr("src",json.imgWrap1_div4.img).appendTo($('.midslideshow .imgWrap4 .warpimg4 a'));
		$('.midslideshow .warpjieshao4 a').append(json.imgWrap1_div4.name)
		$(".midslideshow .warpprice4").append(json.imgWrap1_div4.price)
	})
	var index = 0;
	//右按钮点击事件
	$('.midslideshow_box .btnr').click(function(){
		index ++;
		$('.midslideshow').css("left",(-998 * index) + "px");
		if(index >= 3){
			index = -1;
		}
	})
	//左按钮点击事件
	$('.midslideshow_box .btnl').click(function(){
		index --;
		if(index == -2){
			index = 2;
		}else if(index == -1){
			index = 3;
		}
		$('.midslideshow').css("left",(-998 * index) + "px");
	})
	//商品列表中
	$.getJSON('json/index1.json',function(json){
		$('<img>').attr("src",json.imgWrap1_div5.img).appendTo($('.jlg_list_middle .midtotal .imgWrap5 .warpimg5 a'));
		$('.imgWrap5 .warpjieshao5 a').append(json.imgWrap1_div5.name)
		$(".imgWrap5 .warpprice5").append(json.imgWrap1_div5.price)
		$(".imgWrap5 .infor").append(json.imgWrap1_div5.infor)
	})
	//商品列表下
	$.getJSON('json/index1.json',function(json){
		$('<img>').attr("src",json.imgWrap1_div6.img).appendTo($('.jlg_list_bottom1 .listboxright .right1 a'));
		$('.listboxright .warpjieshao6 a').append(json.imgWrap1_div6.name)
		$(".listboxright .warpprice6").append(json.imgWrap1_div6.price)
		
		$('<img>').attr("src",json.imgWrap1_div7.img).appendTo($('.jlg_list_bottom1 .listboxright .right2 a'));
		$('.listboxright .warpjieshao7 a').append(json.imgWrap1_div7.name)
		$(".listboxright .warpprice7").append(json.imgWrap1_div7.price)
		
		$('<img>').attr("src",json.imgWrap1_div8.img).appendTo($('.jlg_list_bottom1 .listboxright .right3 a'));
		$('.listboxright .warpjieshao8 a').append(json.imgWrap1_div8.name)
		$(".listboxright .warpprice8").append(json.imgWrap1_div8.price)
		
		$('<img>').attr("src",json.imgWrap1_div9.img).appendTo($('.jlg_list_bottom1 .listboxright .right4 a'));
		$('.listboxright .warpjieshao9 a').append(json.imgWrap1_div9.name)
		$(".listboxright .warpprice9").append(json.imgWrap1_div9.price)
	})
	loadCart();
	//购物车显示
	function loadCart(){
		var cartStr = $.cookie('cart') ? $.cookie('cart') : '';
		var cartObj = convertCartStrToObj(cartStr);
		var total = 0;
		for(var id in cartObj){
			total += cartObj[id].num;;
		}
		console.log(cartObj[id])
		$('.count').html(total);
	}
	//将字符串转为对象
	function convertCartStrToObj(cartStr){
		if(!cartStr){
			return {};
		}
		var goods = cartStr.split(':');
		var obj = {};
		for(var i = 0;i < goods.length;i ++){
			var data = goods[i].split(',');
			obj[data[0]] = {
				name : data[1],
				price : parseFloat(data[2]),
				num : parseInt(data[3]),
				src : data[4]					
			}
		}
		return obj;
	}
	//将对象转换为字符串
	function convertObjToCartStr(obj){
		var cartStr = '';
		for(var id in obj){
			if(cartStr){
				cartStr += ':';
			}
			cartStr += id + ',' + obj[id].name + "," + obj[id].price + ',' + obj[id].num + ',' + obj[id].src;
		}
		return cartStr;
	}
})
